package com.wondertek.poms.core.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.wondertek.poms.common.service.impl.ServiceImpl;
import com.wondertek.poms.core.service.IPcOperateLogService;
import com.wondertek.poms.dao.po.PcOperateLog;
import com.wondertek.poms.dao.repo.IPcOperateLogDao;

import cn.hutool.core.util.StrUtil;

/**
 * <p><b>PcOperateLogServiceImpl Description:</b> 操作日志表service层实现 </p>
 * @author 窦再兴
 * <p><b>DATE</b> 2019-08-12 11:20:53</p>
 */
@Service
@Scope("prototype")
public class PcOperateLogServiceImpl extends ServiceImpl<PcOperateLog, Long> implements IPcOperateLogService {

	@Autowired
	private IPcOperateLogDao pcOperateLogDao;

	@Override
	@Transactional
	public PcOperateLog deleteLogic(Long id) {
		return null;
	}

	@Override
	@Transactional
	public PcOperateLog merge(PcOperateLog model) {
		return null;
	}

	@Override
	@Async
	public PcOperateLog save(PcOperateLog entity) {
		return super.save(entity);
	}

	@Override
	public Page<PcOperateLog> findLike(PcOperateLog pcOperateLog, int page, int rows, String sord, String sidx) {
		Pageable pageable = getPageable(page - 1, rows, sidx, sord);
		Specification<PcOperateLog> specification = getWhereClause(pcOperateLog);
		return pcOperateLogDao.findAll(specification, pageable);
	}
	private Specification<PcOperateLog> getWhereClause(PcOperateLog pcOperateLogVo) {
		return new Specification<PcOperateLog>() {

			@Override
			public Predicate toPredicate(Root<PcOperateLog> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> predicate = new ArrayList<>();

				//日志标题、请求地址、日志类型、操作用户、操作时间；
				if (StrUtil.isNotBlank(pcOperateLogVo.getModel())) {
					predicate.add(cb.like(root.get("model"), "%" + pcOperateLogVo.getModel() + "%"));
				}
				if (StrUtil.isNotBlank(pcOperateLogVo.getUrl())) {
					predicate.add(cb.like(root.get("url"), "%" + pcOperateLogVo.getUrl() + "%"));
				}
				if (pcOperateLogVo.getOperateType() != null) {
					predicate.add(cb.equal(root.get("operateType"), pcOperateLogVo.getOperateType()));
				}
				if (StrUtil.isNotBlank(pcOperateLogVo.getUserName())) {
					predicate.add(cb.like(root.get("userName"), "%" + pcOperateLogVo.getUserName() + "%"));
				}
				if (StrUtil.isNotBlank(pcOperateLogVo.getParams())) {
					predicate.add(cb.like(root.get("params"), "%" + pcOperateLogVo.getParams() + "%"));
				}
				if (pcOperateLogVo.getStartTime() != null) {
					predicate.add(cb.greaterThanOrEqualTo(root.get("createdTime"), pcOperateLogVo.getStartTime()));
				}
				if (pcOperateLogVo.getEndTime() != null) {
					predicate.add(cb.lessThanOrEqualTo(root.get("createdTime"), pcOperateLogVo.getEndTime()));
				}
				
				Predicate[] pre = new Predicate[predicate.size()];
				return query.where(predicate.toArray(pre)).getRestriction();
			}
		};
	}
}